package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();

    }

    @GetMapping
    public Result query(String name, Integer status, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize)
    {  log.info("查询部门:{},{},{},{}", name, status,page, pageSize);
        PageResult<Dept> pageResult = deptService.query(name, status, page, pageSize);

        return Result.success(pageResult);

    }
    @Log
    @GetMapping("/{id}")
    public Result SelectById(@PathVariable Integer id) {
        log.info("查询部门:{}", id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);

    }
    @Log
    @PutMapping
    public Result updata(@RequestBody Dept dept) {
        log.info("更新部门:{}", dept);
        deptService.updateDept(dept);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门:{}", id);
        deptService.delete(id);
        return Result.success();

    }


    @GetMapping("/list")
    public Result findAll(){
        //查询全部数据
        log.info("查询的所有数据{}");
        List<Dept> allData = deptService.findAll();
        return  Result.success(allData);
    }


}
